
<template>
  <el-select 
    style="width:100%"
    v-model="mValue"
    :placeholder="placeholder" 
    transfer :disabled="disabled" 
    :filterable="filterable" 
    :clearable="clearable"
    @change="handleChange" 
    :multiple="multiple" 
    :multiple-limit="multipleLimit">
    <el-option v-for="(item,index) in codeData" :label="item.text" :value="item.value" :key="index" :disabled="in_array(item.value,disabledKeys)"></el-option>
  </el-select>
</template>
<script>
import quickTool from '@/assets/js/quick/quickTool.js'
import {codeProvider} from '@/assets/js/quick/provider.js'
export default {
  name: 'bms-select',
  mixins: [quickTool],
  props:{
    value : {type: String},   // value 如果不是字符串，切换无效果
    clearable : {type:Boolean,default:true},
    filterable : {type:Boolean,default:false},
    disabled : {type:Boolean,default:false},
    codeType : {type:String ,default:''},
    placeholder : {type:String ,default:'请选择'},
    condition : {type:Object,default:()=>{}},
    multiple : {type:Boolean,default:false},
    multipleLimit : {type:Number,default:0},
    disabledKeys : Array
  },
  data(){
    return {
      mValue: this.value,
      codeData : []
    }
  },
  methods : {
    handleChange(val) {
      let tmp = [];
      if(this.multiple) {
        (val || []).forEach(value => {
          this.codeData.forEach(item=>{
            if(item.value == value){
              tmp.push(item);
            }
          });
        })
      } else {
        tmp = "";
        this.codeData.forEach(item => {
          if(item.value == val){
            tmp = item;
            return
          }
        });
      }
      this.$emit('change', tmp);
      this.$emit('input', this.mValue);
    },
    async initData() {
      if(this.codeType) {
        let _this = this
        await codeProvider.getList(this.codeType,this.condition || {}).then(resp => {
          _this.codeData = resp
        }).catch(err => {
          _g.toastMsg("error",err)
        })
      } else {
        console.error('codeType is not empty!');
      }
    }
  },
  watch : {
    value: {
      handler(newValue,oldValue) {
        this.mValue = newValue
        this.initData();
      },
      deep: true
    },
    codeType: {
      handler(newValue,oldValue) {
        this.initData();
      },
      deep: true
    },
    condition: {
      handler(newValue,oldValue) {
        this.initData();
      },
      deep: true
    },
  },
  created() {
    this.initData();
  },
  mounted(){
  },
}
</script>

<style lang="scss" scoped>

</style>